package com.chengfei.house.web.interceptor;

import com.chengfei.house.common.model.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * @author chengfei cheng.fei@chinaott.onaliyun.com
 **/
public class AuthActionInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user =  UserContext.getUser();
        if(user == null){
            String msg = URLEncoder.encode("请先登录","UTF-8");
            String target = URLEncoder.encode(request.getRequestURL().toString(),"utf-8");
            if("GET".equalsIgnoreCase(request.getMethod())){
                response.sendRedirect("/accounts/signin?errorMsg="+msg+"&target="+target);
            }else{
                response.sendRedirect("/accounts/signin?errorMsg="+msg);
            }
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
